import Vue from 'vue'
import VueI18n from 'vue-i18n'
import locale from 'element-ui/lib/locale'
import zhCN from './langs/zh_CN'
import zhTW from './langs/zh_TW'
import enUS from './langs/en_US'
import enLocale from 'element-ui/lib/locale/lang/en'
import zhCnLocale from 'element-ui/lib/locale/lang/zh-CN'
import zhTwLocale from 'element-ui/lib/locale/lang/zh-TW'

export function createI18n(obj) {
  Vue.use(VueI18n)
  let messages
  if (obj) {
    messages = {
      en_US: Object.assign(
        obj.enUS || {},
        enUS || {},
        enLocale
      ),
      zh_TW: Object.assign(
        obj.zhTW || {},
        zhTW || {},
        zhTwLocale
      ),
      zh_CN: Object.assign(
        obj.zhCN || {},
        zhCN || {},
        zhCnLocale
      )
    }
  } else {
    messages = {
      en_US: Object.assign(enUS || {}, enLocale),
      zh_TW: Object.assign(zhTW || {}, zhTwLocale),
      zh_CN: Object.assign(zhCN || {}, zhCnLocale)
    }
  }
  const i18n = new VueI18n({
    locale: localStorage.getItem('locale') || 'zh_CN',
    messages
  })

  locale.i18n((key, value) => i18n.t(key, value)) // 为了实现element插件的多语言切换
  return i18n
}
